<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;


class User extends Admincoetroller
{
    //加载后台首页
    public function index()
    {
        return view('user/index');

    }

    //浏览管理员
    public function userlist()
    {   
        

        // 查询状态为1的用户数据 并且每页显示10条数据
        $list = Db::name('user')->paginate(1000);
        // 把分页数据赋值给模板变量list
        $this->assign('list', $list);

        // echo '<pre>';
        //  print_r($this);
        // echo '</pre>';

    // die;
        // 渲染模板输出
        return $this->fetch('user/userlist',['list'=>$list]);

    }

    //搜索管理员
    public function search(Request $request)
    {
         $req = $request->post();
         // var_dump($req);die;

        // 查询状态为1的用户数据 并且每页显示10条数据
        
        $list = Db::name('user')->where('username',$req['username'])->paginate(1);
        // 把分页数据赋值给模板变量list
        $this->assign('list', $list);

        // 渲染模板输出
        return $this->fetch('user/userlist',['list'=>$list]);

    }

    //加载添加管理员页面
    public function add()
    {
        
        return $this->fetch('user/add');
    }

    //添加管理员  
    public function address(Request $request)
    {   
        $req = $request->post();

        $req['password']=md5($req['password']);

        $data = ['username' => $req['user'], 'name' => $req['ruser'],'userpass' => $req['password'] ];
        $sql=Db::name('user')->insert($data);


        return $this->success('成功',url('admin/user/userlist'));
    }

    //删除管理员
     public function beleteuser($id)
    {
        // var_dump($id);
        // return'111';
        // die;
        $sql=Db::name('user')->delete(['id'=>$id]);
        return $this->success('删除成功');

    }

      // 加载分配角色页面
    public function getuser($id)
    {
        $list=Db::name('user')->field('name')->where('username="'.Session::get('name').'"')->find();

        $nodes = Db::name('role')->where('status', 1)->select();
        //根据id查出rid
        $nodelist = Db::name('user_role')->where('uid',$id)->select();
        $nodelists = array();
        foreach ($nodelist as $v){
            $nodelists[] = $v['rid'];
        }
        return view('user/disuser',[
            "nodes" => $nodes,
            "id" => $id,
            'nodelist' => $nodelists
        ]);

        
    }

        //分配角色页面
    public function roleuser(Request $request)
    {   
        $sql = $request->post();
        if(empty($sql['nid'])){
            return $this->error('必须选择至少一个角色');
        }

        $nids = array();
        foreach ($sql['nid'] as $k => $v) {
            $nids[] = $v;
        }
        // var_dump($sql);
        $uid=$sql['rid'][0];

        // var_dump($nids);
        // var_dump($uid);  //id
        // die;
        
       
        Db::name('user_role')->where('uid = '.$uid)->delete();


        foreach ($nids as $k => $v) {

                $role_node['rid'] = $v;
                $role_node['uid'] = $uid;
                $data = Db::name('user_role')->data($role_node)->insert();
        }
        return $this->success('分配成功',url('user/userlist'));
        // var_dump($role_node['rid']);
        // var_dump($role_node['uid']);

        // var_dump($sqls);
        // die;
    }


    //修改个人资料
   public function modifyuser($id)
   {
        // $sql=Db::name('user')->update(['name' => 'thinkphp','id'=>1]);
        // 
        // return view('user/modify');
        $list=Db::name('user')->field('name')->where('username="'.Session::get('name').'"')->find();
        // $sql=Db::naem('user')->field('')->where('id='.$id)->select();
        $sql=Db::name('user')->where(['id' => $id])->findOrFail();
        // var_dump($sql['id']);
        // die;
        return $this->fetch('user/modify',['name'=>$list['name'],
                            'username'=>$sql['username'], 
                            'name'=>$sql['name'],
                            'id'=>$sql['id']
                            ]);

   }

    
    
}
